<!DOCTYPE html>
<html>
<head>
    <title>Linear Momentum vs. Time Graph</title>
</head>
<body>
    <canvas id="physicsCanvas" width="550" height="450"></canvas>
    <script>
        const canvas = document.getElementById('physicsCanvas');
        const ctx = canvas.getContext('2d');

        // Define graph area and margins
        const margin = { top: 40, right: 30, bottom: 60, left: 80 };
        const graphWidth = canvas.width - margin.left - margin.right;
        const graphHeight = canvas.height - margin.top - margin.bottom;

        // Data ranges
        const xMax = 10;
        const yMax = 10;

        // Coordinate transformation functions
        function xPos(time) {
            return margin.left + (time / xMax) * graphWidth;
        }

        function yPos(momentum) {
            return margin.top + graphHeight - (momentum / yMax) * graphHeight;
        }

        // Clear canvas
        ctx.fillStyle = 'white';
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        // --- Draw Grid ---
        ctx.strokeStyle = '#d3d3d3'; // Light gray for grid lines
        ctx.lineWidth = 1;

        // Vertical grid lines
        for (let i = 1; i <= xMax; i++) {
            ctx.beginPath();
            ctx.moveTo(xPos(i), yPos(0));
            ctx.lineTo(xPos(i), yPos(yMax));
            ctx.stroke();
        }

        // Horizontal grid lines
        for (let i = 1; i <= yMax; i++) {
            ctx.beginPath();
            ctx.moveTo(xPos(0), yPos(i));
            ctx.lineTo(xPos(xMax), yPos(i));
            ctx.stroke();
        }
        
        // --- Draw Axes ---
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 2;

        // X-axis
        ctx.beginPath();
        ctx.moveTo(margin.left, yPos(0));
        ctx.lineTo(margin.left + graphWidth, yPos(0));
        ctx.stroke();

        // Y-axis
        ctx.beginPath();
        ctx.moveTo(xPos(0), margin.top);
        ctx.lineTo(xPos(0), margin.top + graphHeight);
        ctx.stroke();

        // --- Draw Ticks and Labels ---
        ctx.fillStyle = 'black';
        ctx.font = '14px Arial';
        
        // X-axis ticks and labels
        ctx.textAlign = 'center';
        ctx.textBaseline = 'top';
        for (let i = 0; i <= xMax; i += 2) {
            ctx.fillText(i, xPos(i), yPos(0) + 8);
        }
        ctx.font = '16px Arial';
        ctx.fillText('Time (s)', xPos(xMax / 2), yPos(0) + 35);
        
        // Y-axis ticks and labels
        ctx.font = '14px Arial';
        ctx.textAlign = 'right';
        ctx.textBaseline = 'middle';
        for (let i = 0; i <= yMax; i += 5) {
             if (i !== 0) { // Avoid drawing 0 twice
                ctx.fillText(i, xPos(0) - 8, yPos(i));
            }
        }
        ctx.fillText('10', xPos(0) - 8, yPos(10));
        
        // Y-axis title
        ctx.save();
        ctx.translate(margin.left - 60, margin.top + graphHeight / 2);
        ctx.rotate(-Math.PI / 2);
        ctx.textAlign = 'center';
        ctx.font = '16px Arial';
        ctx.fillText('Magnitude of Linear Momentum (kg m/s)', 0, 0);
        ctx.restore();

        // --- Draw Data Lines ---
        ctx.lineWidth = 3;

        // Object 1 (Red line)
        ctx.strokeStyle = 'red';
        ctx.beginPath();
        ctx.moveTo(xPos(0), yPos(8));
        ctx.lineTo(xPos(2), yPos(8));
        ctx.lineTo(xPos(6), yPos(5));
        ctx.stroke();

        // Object 2 (Blue line)
        ctx.strokeStyle = 'blue';
        ctx.beginPath();
        ctx.moveTo(xPos(0), yPos(2));
        ctx.lineTo(xPos(2), yPos(2));
        ctx.lineTo(xPos(6), yPos(5));
        ctx.stroke();
        
        // Combined object (Purple line)
        ctx.strokeStyle = 'purple';
        ctx.beginPath();
        ctx.moveTo(xPos(6), yPos(5));
        ctx.lineTo(xPos(10), yPos(5));
        ctx.stroke();

        // --- Add Object Labels ---
        ctx.fillStyle = 'black';
        ctx.font = 'bold 14px Arial';
        ctx.textAlign = 'left';
        
        // Object 1 label
        ctx.textBaseline = 'bottom';
        ctx.fillText('Object 1', xPos(0.4), yPos(8) - 5);
        
        // Object 2 label
        ctx.textBaseline = 'top';
        ctx.fillText('Object 2', xPos(0.4), yPos(2) + 5);

    </script>
</body>
</html>